// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var leftMenu = require("../leftMenu/leftMenu");
    var topMenu = require("../topMenu/topMenu");
    var router = require("../hash/pageRouter");
    var footer = require("../footer/footer_main");
    var dialog = require("dialog");
    var validate = require("validate");
    var constant = require("constant");
    var databind = require("databind");
    var firstVisited = 0;
    var tools = require("tools");
    var auditState;
    var ym;
    var leftM;
    var year;
	var month;
    var pageIndex = 1,
        pageSize = 10,
        totalPageCount = 1,
        totalRecordCount = 0; //分页初始化
        
    var init = function(){
    	$("#header").html(require("../topMenu/topMenu.html"));
    	firstVisited = base.getUrlParam("firstVisited");
    	leftM = auditState = base.getUrlParam("leftM");
    	leftM = leftM == null?4:leftM;
    	footer.init();  	
    	$("#footer").removeClass("none");

    	leftMenu.init(leftM);
    	
    	$(".serviceFeeIncome").parents("li.leftMenu-item").addClass("leftMenu-open");
    	$(".leftMenu-item").removeClass("leftMenu-active");
    	$(".serviceChargeSettlement").addClass("leftMenu-active");
    	
    	topMenu.init(2);
    	$(".footer").removeClass("none");
    	
    	selectMonthBillDetailNew(); // 月汇总收益信息
		initEvent();
		router.init();
		
		if(firstVisited != 1){
			$(".table-section").removeClass("none");
		}
    };
    
	var initEvent = function(){
		// 出账账单样式
		$("#serviceChargeSettlement").on("click", ".billInfo", function() {
			$(this).css("color", "red");
			$("#serviceChargeSettlement .searchInfo").css("color", "black");
			$(".panel2_left").css("width", "1%");
			$(".panel2_right").css("width", "88%");
			
			$("#serviceChargeSettlement .billInfoDiv").show();
			$("#serviceChargeSettlement .tableSearch").hide();
			$(".billInfo").prev().attr("class", "backward");
			$(".billInfo").next().attr("class", "forward");
			// 最新账单汇总信息和企业信息
			selectMonthBillDetailNew();
			inquiryMonthEarningInfoList(true, year, month, null, null, null, null); // 企业月账单信息
		})
		
		// 查询样式
		$("#serviceChargeSettlement").on("click", ".searchInfo", function() {
			$(this).css("color", "red");
			$("#serviceChargeSettlement .billInfo").css("color", "black");
			$(".panel2_left").css("width", "20%");
			$(".panel2_right").css("width", "68%");
			
			$("#serviceChargeSettlement .billInfoDiv").hide();
			$("#serviceChargeSettlement .tableSearch").show();
			
			// 禁用前后查询
			$(".billInfo").prev().attr("class", "");
			$(".billInfo").next().attr("class", "");
		})
		
		// 详情
		$("#serviceChargeSettlement").on("click", ".table_details_link", function() {
			var year = $(this).parent("tr.tr_style").attr("year");
			var month = $(this).parent("tr.tr_style").attr("month");
			var monthEarningNumber = $(this).parent("tr.tr_style").attr("monthEarningNumber");
			var enterpriseId = $(this).parent("tr.tr_style").attr("enterpriseId");
			location.href="../client/settlementDetail.html?year=" + year + "&month=" + month + "&monthEarningNumber=" + monthEarningNumber + "&enterpriseId=" + enterpriseId;
		});
		
		// 向前
		$("#serviceChargeSettlement").on("click", ".forward", function() {
			forwardOrBackward(true);
		})
		
		// 向后
		$("#serviceChargeSettlement").on("click", ".backward", function() {
			forwardOrBackward(false);
		})
		
		// 企业月账单列表
		$("#serviceChargeSettlement").on("click", "#searchMonthEarningList", function() {
			var monthEarningNumber = $("#serviceChargeSettlement input[name=monthEarningNumber]").val();
	    	var enterpriseName = $("#serviceChargeSettlement input[name=enterpriseName]").val();
	    	var earningDateStar = $("#serviceChargeSettlement input[name=earningDateStar]").val();
	    	var earningDateEnd = $("#serviceChargeSettlement input[name=earningDateEnd]").val();
			inquiryMonthEarningInfoList(true, null, null, monthEarningNumber, enterpriseName, earningDateStar, earningDateEnd, function(data) {
				
			});
		})
    };
    
    // 最新汇总月账单信息
    var selectMonthBillDetailNew = function() {
    	base.fnAjax({
			action: "selectMonthBillDetailNew.json",
			async: false,
			nocode: true,
			oData: {
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(data.datas.length == 0) {
						base.alert(2, "没有最新账单信息");
						return;
					}else if(data.datas.length == 1) {
						// 总资产和总服务费信息
						var bulkstockValuationTotal = data.datas[0].bulkstockValuation2 + data.datas[0].bulkstockValuation5 + data.datas[0].bulkstockValuation6;
						var bulkstockIntrerstTotal = data.datas[0].bulkstockIntrerst2 + data.datas[0].bulkstockIntrerst5 + data.datas[0].bulkstockIntrerst6;
						data.datas[0].bulkstockValuationTotal = bulkstockValuationTotal;
						data.datas[0].bulkstockIntrerstTotal = bulkstockIntrerstTotal;
						
						juicer.register("formatCurrency", tools.formatCurrency);
						
						var tpl = $("#monthBillDetailTemp").html();
	                    var html = juicer(tpl, data);
	                    $("#monthBillDetailBody").html(html);
	                    
	                    year = data.datas[0].year;
						month = data.datas[0].month;
	                    $("#serviceChargeSettlement .yearMonth").text(year + "-" + month);
	                    
	                    inquiryMonthEarningInfoList(true, year, month, null, null, null, null); // 企业月账单信息
					}
				}else {
					base.alert(2, "信息获取失败");
				}
			}
		});
    };
    
    // 汇总月账单信息
    var selectMonthBillDetail = function(year, month, callback) {
    	base.fnAjax({
			action: "selectMonthBillDetail.json",
			async: true,
			nocode: true,
			oData: {
				year: year,
				month: month
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var bulkstockValuationTotal;
					var bulkstockIntrerstTotal;
					if(data.datas.length == 0) {
						// 总资产和总服务费信息
						bulkstockValuationTotal = null;
						bulkstockIntrerstTotal = null;
					}else {
						// 总资产和总服务费信息
						bulkstockValuationTotal = data.datas[0].bulkstockValuation2 + data.datas[0].bulkstockValuation5 + data.datas[0].bulkstockValuation6;
						bulkstockIntrerstTotal = data.datas[0].bulkstockIntrerst2 + data.datas[0].bulkstockIntrerst5 + data.datas[0].bulkstockIntrerst6;
						data.datas[0].bulkstockValuationTotal = bulkstockValuationTotal;
						data.datas[0].bulkstockIntrerstTotal = bulkstockIntrerstTotal;
						year = data.datas[0].year;
						month = data.datas[0].month;
						
						juicer.register("formatCurrency", tools.formatCurrency);
				
						var tpl = $("#monthBillDetailTemp").html();
	                    var html = juicer(tpl, data);
	                    $("#monthBillDetailBody").html(html);
	                    
	                    $("#serviceChargeSettlement .yearMonth").text(year + "-" + month);
					}
					callback(data);
				}else {
					base.alert(2, "信息获取失败");
				}
			}
		});
    };
    
    // 企业月账单信息
    var inquiryMonthEarningInfoList = function(isSearchbutton, year, month, monthEarningNumber, enterpriseName, earningDateStar, earningDateEnd, callback) {
    	base.fnAjax({
			action: "inquiryMonthEarningPageList.json",
			async: false,
			nocode: true,
			oData: {
				pageIndex: pageIndex,
        		pageSize: pageSize,
				year: year, // 账单年
				month: month, // 账单月
				monthEarningNumber: monthEarningNumber, //结算编号
				enterpriseName: enterpriseName, 
				earningDateStar: earningDateStar,
				earningDateEnd: earningDateEnd
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					if(isSearchbutton == true) { // 查询按钮
						juicer.register("formatCurrency", tools.formatCurrency);
						
						var tpl = $("#monthEarning").html();
	                    var html = juicer(tpl, data);
	                    $("#monthEarningBody").html(html);
	                    
	                    totalPageCount = data.totalPageCount;
	                    totalRecordCount = data.totalRecordCount;
	                    base.pageControl(
	                    	$("#monthEarningPage"),
	                        totalRecordCount,
	                        data.pageIndex,
	                        data.pageSize,
	                        function(p) {
	                            pageIndex = p;
	                            inquiryMonthEarningInfoList();
	                    });
					}else { // 前后箭头查询
						if(data.datas.length == 0) {
							callback(data);
						}else {
							juicer.register("formatCurrency", tools.formatCurrency);
						
							var tpl = $("#monthEarning").html();
		                    var html = juicer(tpl, data);
		                    $("#monthEarningBody").html(html);
		                    
		                    totalPageCount = data.totalPageCount;
		                    totalRecordCount = data.totalRecordCount;
		                    base.pageControl(
		                    	$("#monthEarningPage"),
		                        totalRecordCount,
		                        data.pageIndex,
		                        data.pageSize,
		                        function(p) {
		                            pageIndex = p;
		                            inquiryMonthEarningInfoList();
		                    });
						}
					}
					
                    //企业名称
					$("#enterpriseName").unbind("click").click(function() {
						router.route("client/clientManage_details.html?enterpriseId="+$(this).parents("tr").attr("enterpriseId"));
					});
				}else {
					base.alert(2, "信息获取失败");
				}
			}
		})
    };
    
    //向前或向后
    var forwardOrBackward = function(forward) {
    	var yearLater;
    	var monthLater;
    	var yearMonthLaterStr;
    	var today;
    	if(forward == true) { //向前
    		monthLater = month + 1;
    		if(monthLater > 1 && monthLater < 13) {
    			yearLater = year;
    		}else if(monthLater == 13) {
    			monthLater = 1;
    			yearLater = year + 1;
    		}
    	}else { // 向后
    		monthLater = month - 1;
    		if(monthLater == 0) {
    			monthLater = 12;
    			yearLater = year - 1;
    		}else if(monthLater > 0 && monthLater < 12) {
    			yearLater = year;
    		}
    	}
    	// 月份"09"
    	if(monthLater < 10) {
    		monthLater = "0" + monthLater;
    	}
    	// 月账单汇总信息和月账单企业信息
    	selectMonthBillDetail(yearLater, monthLater, function(data) {
    		if(data.datas.length == 0) {
    			base.alert(2, "没有账单了");
    			return;
    		}else {
    			// 当前账单年月
		    	year = yearLater;
		    	month = monthLater;
		    	yearMonthLaterStr = yearLater + "-" + monthLater;
    			$("#serviceChargeSettlement .yearMonth").text(yearMonthLaterStr);
    		}
    	});
    	inquiryMonthEarningInfoList(false,yearLater, monthLater, null, null, null, null, function(data) {
    		
    	});
    };
    
    return {
        init:init
    };
});